package com.neusoft.yuejian_01.activity.MJJ;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian_01.R;
import com.neusoft.yuejian_01.adapter.CommentListAdapter;
import com.neusoft.yuejian_01.base.BaseActivity;
import com.neusoft.yuejian_01.javabean.CollectBean;
import com.neusoft.yuejian_01.javabean.CommentListBean;
import com.neusoft.yuejian_01.javabean.IsCollectBean;
import com.neusoft.yuejian_01.javabean.setCommentBean;
import com.neusoft.yuejian_01.listener.RetrofitListener;
import com.neusoft.yuejian_01.model.CollectModel;
import com.neusoft.yuejian_01.model.Const;
import com.neusoft.yuejian_01.model.ConstPhoto;
import com.neusoft.yuejian_01.model.SetPictureModel;

import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;

import static com.neusoft.yuejian_01.R.id.likee;

/**
 * Created by Administrator on 2017/10/9.
 */

public class SetPictureInfoActivity extends BaseActivity implements RetrofitListener{

    private RecyclerView recyclerView;
    private int p_id;
    private CommentListAdapter adapter;
    private TextView UserName, content, setTime, ClubName;
    private ImageView back, setPicture1eft, setPictureright,touxiang,Collectm;
    private Button setComment;
    private EditText commentInfo;
    private boolean flags;
    //判断是否关注：id 帖子id，5=帖子
    private void  ifcollect(int u_id, String select_id){
        CollectModel collectModel = new CollectModel();
        collectModel.ifCollected(String.valueOf(u_id),String.valueOf(select_id), "5",this);
    }

//xml控件
    protected void initView() {
        setContentView(R.layout.activity_set_picture_info);
        recyclerView=(RecyclerView)findViewById(R.id.commentlist);
        recyclerView= (RecyclerView)findViewById(R.id.commentlist);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        UserName=(TextView)findViewById(R.id.text_userName);
        content=(TextView)findViewById(R.id.text_content);
        setTime=(TextView)findViewById(R.id.textView_set_time);
        ClubName=(TextView)findViewById(R.id.textView_club_name);
        back=(ImageView)findViewById(R.id.back);
        Collectm=(ImageView)findViewById(likee);
        touxiang=(ImageView)findViewById(R.id.touxiang);
        setPicture1eft=(ImageView)findViewById(R.id.imageView_set);
        setPictureright=(ImageView)findViewById(R.id.imageView_set1);
        setComment= (Button) findViewById(R.id.addtcomment);
        commentInfo= (EditText) findViewById(R.id.writeComment);

    }

//xml赋值
    protected void initEvents() {
        Bundle bundle=this.getIntent().getExtras();
        final int  pid=bundle.getInt("p_id");
        String ptitle=bundle.getString("p_title");
        String u_name=bundle.getString("u_name");
        String c_name=bundle.getString("c_name");
        String createtime=bundle.getString("p_createtime");
        String headpic= ConstPhoto.BASE_URL+ConstPhoto.SetPictureFile+bundle.getString("u_pic");
        String pic= ConstPhoto.BASE_URL+ConstPhoto.SetPicturePFile+bundle.getString("p_pic");
        String pic1= ConstPhoto.BASE_URL+ConstPhoto.SetPicturePFile+bundle.getString("p_pic1");
        Picasso.with(this)
                .load(headpic)
                .into(touxiang);
        Picasso.with(this)
                .load(pic)
                .into(setPicture1eft);
         Picasso.with(this)
                .load(pic1)
                .into(setPictureright);
        content.setText(ptitle);
        setTime.setText(createtime);
        UserName.setText(u_name);
        ClubName.setText(c_name);
 }


//评论列表 发布评论 判断是否收藏 以及收藏请求
    @Override
    protected void initData() {
//  显示评论列表
        SetPictureModel setPictureModel=new SetPictureModel();
        Bundle bundle=this.getIntent().getExtras();
        final int pid=bundle.getInt("p_id");
        setPictureModel.getCommentlist(pid,this);
 //点击收藏
        Collectm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CollectModel collectModel = new CollectModel();
                collectModel.doCollect(getUser_id(),String.valueOf(pid),SetPictureInfoActivity.this);
            }
        });
 //判断是否收藏
        ifcollect(getUser_id(),String.valueOf(pid));

 //发送对于晒图评论
        setComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String commentinfo1 =commentInfo.getText().toString();
                Log.e("xxxxxxxxxx",commentinfo1+"-------");
                if (!commentinfo1.equals("")&&commentinfo1!=null) {
                    SetPictureModel setPictureModel=new SetPictureModel();
                    setPictureModel.addcomment(getUser_id(),pid,commentinfo1,SetPictureInfoActivity.this);
                  }
                else {
                    Toast.makeText(SetPictureInfoActivity.this, "请输入评论内容", Toast.LENGTH_SHORT).show();
                 }
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
    //返回评论列表 发布评论 判断是否收藏 以及收藏请求
    @Override
    public void onSuccess(Object object, int flag) {
        switch (flag){
            case Const.CommentList:
                if (SetPictureInfoActivity.this !=null) {
                    List<CommentListBean> commentListBean=(List<CommentListBean>)object;
                    adapter = new CommentListAdapter(SetPictureInfoActivity.this, commentListBean);
                    LinearLayoutManager layoutManager = new LinearLayoutManager(SetPictureInfoActivity.this);
                    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    recyclerView.setLayoutManager(layoutManager);
                    recyclerView.setAdapter(adapter);
                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                }
                break;
            case Const.DoCollect:
                CollectBean collectBean=(CollectBean)object;
                if (collectBean.getSuccess().equals("1")) {
                    Log.e(collectBean.getSuccess(),"---------");
                    if(flags){
                        flags=false;
                        Collectm.setImageResource(R.drawable.unlike);
                    }else {
                        flags=true;
                        Collectm.setImageResource(R.drawable.like);
                    }
                    Toast.makeText(SetPictureInfoActivity.this, "OK", Toast.LENGTH_SHORT).show();
                }
                break;
            case Const.IfCollect:
                //判断对贴子手收藏
                IsCollectBean isCollectBean=(IsCollectBean)object;
                if (isCollectBean.getCollected().equals("1")){
                    flags =true;
                    Collectm.setImageResource(R.drawable.like);
                }
                else {
                    flags=false;
                    Collectm.setImageResource(R.drawable.unlike);
                }
                break;
            //判断增加评论
            case Const.AddComment:
                setCommentBean setCommentBean =(setCommentBean)object;
                if (setCommentBean.getSuccess().equals("0")){
                    Toast.makeText(SetPictureInfoActivity.this,"评论失败",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(SetPictureInfoActivity.this,"评论成功",Toast.LENGTH_SHORT).show();
                }
                break;

        }
    }

    @Override
    public void onFail() {

    }
}
